<?php
require_once 'config/conf.inc.php';
define("PRODUCT_STATUS_UNUSED", 1);
define("PRODUCT_STATUS_WAITUSE", 2);
define("PRODUCT_STATUS_USING", 3);

Class ProductManager
{
	static function add_product($product_info, &$err)
	{
		$err = "";
			
		if (empty($product_info))
		{
			$err = "params_empty";
			return false;
		}
	
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Product", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}

		$product_name = $product_info['cms_name'];
		$product_status = PRODUCT_STATUS_UNUSED;
		$product_title = $product_info['cms_title'];
		$introduction = mysql_escape_string($product_info['introduction']);
		$price = $product_info['price'];
		$original_price = $product_info['original_price'];
		$start_time = $product_info['start_time'];
		$end_time = $product_info['end_time'];
		$pic_file = $product_info['title_img_name'];
		$product_detail = mysql_escape_string($product_info['product_desc']);
		$address = mysql_escape_string($product_info['address']);
		$bus = mysql_escape_string($product_info['bus']);
		$map_url = "";
		$comment = "";
		$remark = "";
		
		$sql = "insert into Tbl_ProductInfo set FProductName='$product_name', FProductStatus=$product_status, ";
		$sql.= " FProductTitle='$product_title', FIntro='$introduction', FPrice=$price, FOriginalPrice=$original_price, ";
		$sql.= " FStartTime='$start_time', FEndTime='$end_time', FMainPicture='$pic_file',";
		$sql.= " FGroupenDetailInfo='$product_detail', FAddress='$address', FBus='$bus',";
		$sql.= " FGroupenMap='$map_url', FComment='$comment', FRemarks='$remark'";
//echo "$sql";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "insert_err";
			return false;
		}
		
		return true;
	}
	
	static function get_product_id($product_name, &$err)
	{
		$err = "";
			
		if (empty($product_name))
		{
			$err = "params_empty";
			return false;
		}
	
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Product", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql = "select FProductId from Tbl_ProductInfo where FProductName='$product_name'";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "select_err";
			return false;
		}
		$res = $db->fetch_array();
		$product_id = $res['FProductId'];
		if (!empty($product_id))
		{
			return  $product_id;
		}
		
		return 0;
	}
	
	static function get_product_info($product_id, &$product_info, &$err)
	{
		$err = "";
			
		if (empty($product_id))
		{
			$err = "params_empty";
			return false;
		}
	
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Product", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}
		
		$sql = "select * from Tbl_ProductInfo where FProductId='$product_id'";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "select_err";
			return false;
		}
		$res = $db->fetch_array();
		$product_info['cms_name'] = $res["FProductName"];
		$product_info['cms_title'] = $res["FProductTitle"];
		$product_info['introduction'] = $res["FIntro"];
		$product_info['price'] = $res["FPrice"];
		$product_info['original_price'] = $res["FOriginalPrice"];
		$product_info['start_time'] = $res["FStartTime"];
		$product_info['end_time'] = $res["FEndTime"];
		$product_info['product_desc'] = $res["FGroupenDetailInfo"];
		$product_info['address'] = $res["FAddress"];
		$product_info['bus'] = $res["FBus"];
		$product_info['title_img_name'] = $res["FMainPicture"];
		
		return true;
	}
	
	static function set_product_status($product_id, $product_info, &$err)
	{
		$err = "";
			
		if (empty($product_info))
		{
			$err = "params_empty";
			return false;
		}
	
		$db = new Mysql(DB_IP, DB_USER, DB_PASS, "DB_Product", "pconn", "");
		if ($db->is_error)
		{
			$err = "database_err";
			return false;
		}

		$product_name = $product_info['cms_name'];
		$product_status = PRODUCT_STATUS_UNUSED;
		$product_title = $product_info['cms_title'];
		$introduction = mysql_escape_string($product_info['introduction']);
		$price = $product_info['price'];
		$original_price = $product_info['original_price'];
		$start_time = $product_info['start_time'];
		$end_time = $product_info['end_time'];
		$pic_file = $product_info['title_img_name'];
		$product_detail = mysql_escape_string($product_info['product_desc']);
		$address = mysql_escape_string($product_info['address']);
		$bus = mysql_escape_string($product_info['bus']);
		$map_url = "";
		$comment = "";
		$remark = "";
		
		$sql = "insert into Tbl_ProductInfo set FProductName='$product_name', FProductStatus=$product_status, ";
		$sql.= " FProductTitle='$product_title', FIntro='$introduction', FPrice=$price, FOriginalPrice=$original_price, ";
		$sql.= " FStartTime='$start_time', FEndTime='$end_time', FMainPicture='$pic_file',";
		$sql.= " FGroupenDetailInfo='$product_detail', FAddress='$address', FBus='$bus',";
		$sql.= " FGroupenMap='$map_url', FComment='$comment', FRemarks='$remark'";
//echo "$sql";
		$db->query($sql);
		if ($db->is_error)
		{
			$err = "insert_err";
			return false;
		}
		
		return true;
	}
	
	static function delete_product($product_id, &$err)
	{
	
	}
}

?>